<div class="filter">
    <div class="f1 item" style="width: 120px;">
        <input type="text" class="am-form-field" placeholder="起始日期" id="start"
            data-am-datepicker="{theme:'success'}" readonly/>
    </div>
    <div class="f2 item" style="width: 120px;">
        <input type="text" class="am-form-field" placeholder="结束日期" id="end"
            data-am-datepicker="{theme:'success'}" readonly/>
    </div>
    
    <div class="button item">
        <button type="button" class="am-btn am-btn-primary">查看</button>
    </div>
    <div class="clear"></div>
</div>

<div class="red" style="line-height: 24px;">
    【操作提示】资金汇总 = 充值总额 - 提现总额<br/>
    【操作提示】注单盈利 = 投注总额 - 中奖总额 + 撤单返还 - 返点总额<br/>
    【操作提示】实际盈亏 = 注单盈亏 + 惩罚扣款 - 本日奖励
</div>
<table class="am-table am-table-bordered am-table-striped detail-list">
    <thead>
        <tr>
            <th style="text-align: left;">日期</th>
            <th>累计总用户</th>
            <th>新增用户</th>
            <th>新增充值用户</th>
            <th>充值用户</th>
            <th>登录用户</th>
            <th>投注用户</th>
            <th>系统余额</th>
            <th>充值总额</th>
            <th>提现总额</th>
            <th>资金汇总</th>
            <th>投注总额</th>
            <th>中奖总额</th>
            <th>撤单返还</th>
            <th>返点总额</th>
            <th>注单盈亏</th>
            <th>奖励发放</th>
            <th>惩罚扣款</th>
            <th>其他扣款</th>
            <th>实际盈亏</th>
            <th>操作</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<style type="text/css">
    .detail-list th {
        padding: 4px 2px 4px 2px !important;
        text-align: right;
        line-height: 20px;
    }
    .detail-list td {
        text-align: right;
        padding: 4px 2px 4px 2px !important;
        line-height: 20px !important;
    }
    .detail-list .hj td {
        font-weight: bold;
    }
</style>

<!--
<table class="am-table am-table-bordered am-table-striped zjjc">
    <thead>
        <tr>
            <th class="t">平台资金</th>
            <th class="charge"><a href="#">资金流入 <img src="../public/all/images/important.png" height="20px"/></a></th>
            <th>资金流出</th>
            <th>差额</th>
            <th>系统余额</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<div id="ptzj" style="margin: 10px 0px 10px 10px; width: 800px; height: 300px;">
</div>

<table class="am-table am-table-bordered am-table-striped zdtj">
    <thead>
        <tr>
            <th class="t">盈亏报表</th>
            <th>纯利润</th>
            <th>下注总额</th>
            <th>中奖总额</th>
            <th>返水总额</th>
            <th>撤单总额</th>
            <th>注单盈利</th>
            <th>奖励总额</th>
            <th>惩罚扣款</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<div id="ykbb" style="margin: 10px 0px 10px 10px; width: 800px; height: 300px;">
</div>

<table class="am-table am-table-bordered am-table-striped yh">
    <thead>
        <tr>
            <th class="t">用户数据</th>
            <th>累积用户</th>
            <th>新增用户</th>
            <th>充值用户</th>
            <th>新增充值用户</th>
            <th>登录用户</th>
            <th>投注用户</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<div id="yhsj" style="margin: 10px 0px 10px 10px; width: 800px; height: 300px;">
</div>

<style type="text/css">
    .am-table th.t {
        font-size: 14px;
        font-weight: bold;
        color: blue;
    }
</style>
-->

<script>
    $(".f1 input").datepicker({format: 'yyyy-mm-dd'});
    $(".f2 input").datepicker({format: 'yyyy-mm-dd'});

    var t = new Date().getTime();
    var month = new Date().getMonth() + 1;
    $(".f2 input").datepicker("setValue", new Date(t).Format("yyyy-MM-dd"));
    t -= 7 * 24 * 3600 * 1000;
    var month2 = new Date(t).getMonth() + 1;
    if (month2 != month) {
        t = new Date(new Date().getFullYear() + "-" + (month < 10 ? '0' + month : month) + "-01");
    }

    $(".f1 input").datepicker("setValue", new Date(t).Format("yyyy-MM-dd"));
    Util.restorePageFilter();

    $(".item button").click(function() {
        queryDatas();
    });

    var datas = [];
    function queryDatas() {
        var end = $(".f2 input").val();
        var start = $(".f1 input").val();
        var delta = new Date(end).getTime() - new Date(start).getTime();
        if (delta > 31 * 24 * 3600 * 1000) {
            alert2("查询的时间跨度，不能超过1个月！", true);
            return;
        }

        Util.savePageFilter([".f2 input", ".f1 input"]);
        CMD.bet_sum_query("#SUM#", new Date(start).Format("yyyy-MM-dd"), 
        new Date(end).Format("yyyy-MM-dd"), "#SUM#", function(r) {
            r.sort(function(b, a) {
                return a.date.localeCompare(b.date); 
            });
            datas = r;

            startLoading();
            CMD.stat_sum_dquery(start, end, function(r2) {
                // 填入datas
                stopLoading();
                for (var i in r2) {
                    var d = r2[i];
                    for (var j in datas) {
                        if (datas[j].date != d.date) continue;

                        // 合并
                        for (var k in d) {
                            if (k == 'deposit' && typeof(d[k]) == 'string') datas[j][k] = JSON.parse(d[k]);
                            else
                                datas[j][k] = d[k];
                        }
                    }
                }
                fillTables();
            });
        });
    }
    queryDatas();

    // 填充数据
    function fillTables() {
        var table = $(".detail-list tbody");
        table.empty();

        // 先合计汇总下
        var m = {
            userAll: datas[0].userAll,
            balance: datas[0].balance,
            deposit: { total : 0, },
        };
        for (var i in datas) {
            var d = datas[i];
            var f = function(k) {
                if (!m[k]) m[k] = d[k];
                else m[k] += d[k];
            };
            f("userNew");
            f("userDepositFirst");
            f("withdraw");
            f("amount");
            f("win");
            f("back");
            f("profit");
            f("reward");
            f("penalty");
            f("deduct");
            f("userDeposit");
            f("userSignIn");
            f("userBet");

            if (d.deposit && d.deposit.total) m.deposit.total += d.deposit.total;
        }
        var f2 = function(d) {
            var tr = $("<tr></tr>");
            if (Util.isEmpty(d.deposit)) d.deposit = { total: 0 };
            if (!d.date) tr = $("<tr class='hj'></tr>");
            tr.append("<td style='text-align: left;'>" + (d.date || '合计') + "</td>");
            tr.append("<td>" + d.userAll + "</td>");
            tr.append("<td>" + d.userNew + "</td>");
            tr.append("<td>" + d.userDepositFirst + "</td>");
            tr.append("<td>" + d.userDeposit + "</td>");
            tr.append("<td>" + d.userSignIn + "</td>");
            tr.append("<td>" + d.userBet + "</td>");
            tr.append("<td>" + (d.balance/100) + "</td>");
            tr.append("<td>" + (d.deposit.total/100) + "</td>");
            tr.append("<td>" + (d.withdraw/100) + "</td>");

            // 资金汇总
            var v = (d.deposit.total - d.withdraw)/100;
            if (v >= 0)
                tr.append("<td class='red'>" + (v) + "</td>");
            else
                tr.append("<td class='green'>" + (v) + "</td>");

            tr.append("<td>" + (d.amount/100) + "</td>");
            tr.append("<td>" + (d.win/100) + "</td>");
            tr.append("<td>" + (d.back/100) + "</td>");
            tr.append("<td>--</td>"); // TODO: 返点

            var v = d.profit/100;
            if (v >= 0)
                tr.append("<td class='red'>" + (v) + "</td>");
            else
                tr.append("<td class='green'>" + (v) + "</td>");
            tr.append("<td>" + (d.reward/100) + "</td>");
            tr.append("<td>" + (d.penalty/100) + "</td>");
            tr.append("<td>" + (d.deduct/100) + "</td>");

            // 实际盈亏
            var v = (d.profit + d.penalty - d.reward)/100;
            if (v >= 0)
                tr.append("<td class='red'>" + (v) + "</td>");
            else
                tr.append("<td class='green'>" + (v) + "</td>");

            // 操作
            if (!d.date)
                tr.append("<td> </td>");
            else
                tr.append("<td><button class='refresh' value='__v__'>强刷</button></td>".replace(/__v__/, d.date));
            table.append(tr);
        }

        f2(m);
        for (var i in datas) {
            var d = datas[i];
            f2(d);
        }

        $(".refresh").click(function(e) {
            var date = $(e.currentTarget).attr("value");
            if (date) {
                startLoading();
                CMD.stat_sum_restat(date, function() {
                    stopLoading();
                    queryDatas();
                });
            }
        })
    }

    // 资金进入
    function fillMoneyInOut() {
        var table = $(".zjjc tbody");
        table.empty();
        for (var i = 0; i < datas.length; i++) {
            var d = datas[i];
            tr = $("<tr></tr>");
            if (i == 0) tr.append("<td>今天</td>");
            else if (i == 1) tr.append("<td>昨天</td>");
            else if (i == 2) tr.append("<td>前天</td>");
            else tr.append("<td>" + (i + 1) + "天前</td>");
            
            if (d.deposit)
                tr.append("<td>" + ((d.deposit.total || 0)/100).toFixed(2) + "</td>");
            else
                tr.append("<td>0.00</td>");
            tr.append("<td>" + ((d.withdraw || 0)/100).toFixed(2) + "</td>");

            var v = d.deposit ? (d.deposit.total || 0) : 0;
            if (d.withdraw) v -= d.withdraw;

            tr.append("<td>" + (v/100).toFixed(2) + "</td>");
            tr.append("<td>" + ((d.balance || 0)/100).toFixed(2) + "</td>");

            table.append(tr);
        }

        // 图表
        var series = [];
        var f = function(title, cb) {
            var data = {
                name: title,
                type: 'line',
                stack: '总额',
                data: [],
            }
            for (var i = datas.length - 1; i >= 0; i--) {
                data.data.push(cb(datas[i]));
            }
            series.push(data);
        }
        f('资金流入', function(d) {
            var v = 0;
            if (d.deposit) v += d.deposit;
            if (d.reward) v += d.reward;
            return v/100;
        });
        f('资金流出', function(d) {
            var v = 0;
            if (d.withdraw) v += d.withdraw;
            if (d.penalty) v += d.penalty;
            if (d.deduct) v += d.deduct;
            return v/100;
        });
        f('差额', function(d) {
            var v = 0;
            if (d.deposit) v += d.deposit;
            if (d.reward) v += d.reward;

            if (d.withdraw) v -= d.withdraw;
            if (d.penalty) v -= d.penalty;
            if (d.deduct) v -= d.deduct;
            return v/100;
        });
        f('系统余额', function(d) {
            return (d.balance || 0)/100;
        });

        var c = echarts.init($("#ptzj")[0]);
        var option = {
            title: {
                text: "平台资金",
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['资金流入', '资金流出', '差额', '系统余额'],
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['6天前', '5天前', '4天前', '3天前', '前天', '昨天', '今天',],
            },
            yAxis: {
                type: 'value',
            },
            series: series,
        };
        c.setOption(option)
    }

    // 盈亏总览
    function winLose() {
        var table = $(".zdtj tbody");
        table.empty();
        for (var i = 0; i < datas.length; i++) {
            var d = datas[i];
            tr = $("<tr></tr>");
            if (i == 0) tr.append("<td>今天</td>");
            else if (i == 1) tr.append("<td>昨天</td>");
            else if (i == 2) tr.append("<td>前天</td>");
            else tr.append("<td>" + (i + 1) + "天前</td>");

            var v = 0;
            if (d.profit) v = d.profit;
            if (d.penalty) v += d.penalty;
            if (d.back) v -= d.back;
            if (d.reward) v -= d.reward;
            tr.append("<td>" + (v/100).toFixed(2) + "</td>");
            tr.append("<td>" + (d.amount/100).toFixed(2) + "</td>");
            tr.append("<td>" + (d.win/100).toFixed(2) + "</td>");
            tr.append("<td>" + (d.back/100).toFixed(2) + "</td>");
            tr.append("<td>" + ((d.cancel || 0)/100).toFixed(2) + "</td>");
            if (d.profit >= 0)
                tr.append("<td class='red'>" + ((d.profit || 0)/100).toFixed(2) + "</td>");
            else 
                tr.append("<td class='green'>" + ((d.profit || 0)/100).toFixed(2) + "</td>");
            tr.append("<td>" + ((d.reward || 0)/100).toFixed(2) + "</td>");
            tr.append("<td>" + ((d.penalty || 0)/100).toFixed(2) + "</td>");

            table.append(tr);
        }

        // 图表
        var series = [];
        var f = function(title, cb) {
            var data = {
                name: title,
                type: 'line',
                stack: '总额',
                data: [],
            }
            for (var i = datas.length - 1; i >= 0; i--) {
                data.data.push(cb(datas[i]));
            }
            series.push(data);
        }
        f('纯利润', function(d) {
            var v = 0;
            if (d.profit) v = d.profit;
            if (d.penalty) v += d.penalty;
            if (d.back) v -= d.back;
            if (d.reward) v -= d.reward;
            return (v/100).toFixed(2);
        });
        f('下注', function(d) {
            return (d.amount/100).toFixed(2);
        });
        f('返水', function(d) {
            return (d.back/100).toFixed(2);
        });
        f('撤单', function(d) {
            return ((d.cancel || 0)/100).toFixed(2);
        });
        f('注单盈利', function(d) {
            return (d.profit/100).toFixed(2);
        });
        f('奖励', function(d) {
            return ((d.reward || 0)/100).toFixed(2);
        });
        f('扣款', function(d) {
            return ((d.penalty || 0)/100).toFixed(2);
        });

        var c = echarts.init($("#ykbb")[0]);
        var option = {
            title: {
                text: "盈亏报表",
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['纯利润', '下注', '返水', '撤单', '注单盈利', '奖励', '扣款'],
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['6天前', '5天前', '4天前', '3天前', '前天', '昨天', '今天',],
            },
            yAxis: {
                type: 'value',
            },
            series: series,
        };
        c.setOption(option)
    }

    // 用户数据
    function userDatas() {
        var table = $(".yh tbody");
        table.empty();
        for (var i = 0; i < datas.length; i++) {
            var d = datas[i];
            tr = $("<tr></tr>");
            if (i == 0) tr.append("<td>今天</td>");
            else if (i == 1) tr.append("<td>昨天</td>");
            else if (i == 2) tr.append("<td>前天</td>");
            else tr.append("<td>" + (i + 1) + "天前</td>");

            tr.append("<td>" + (d.userAll || 0) + "</td>");
            tr.append("<td>" + (d.userNew || 0) + "</td>");
            tr.append("<td>" + (d.userDeposit || 0) + "</td>");
            tr.append("<td>" + (d.userDepositFirst || 0) + "</td>");
            tr.append("<td>" + (d.userSignIn || 0) + "</td>");
            tr.append("<td>" + (d.userBet || 0) + "</td>");

            table.append(tr);
        }

        // 图表
        var series = [];
        var f = function(title, cb) {
            var data = {
                name: title,
                type: 'line',
                stack: '总额',
                data: [],
            }
            for (var i = datas.length - 1; i >= 0; i--) {
                data.data.push(cb(datas[i]));
            }
            series.push(data);
        }
        f('累积用户', function(d) {
            return d.userAll || 0;
        });
        f('新增用户', function(d) {
            return d.userNew || 0;
        });
        f('充值用户', function(d) {
            return d.userDeposit || 0;
        });
        f('新增充值用户', function(d) {
            return d.userDepositFirst || 0;
        });
        f('登录用户', function(d) {
            return d.userSignIn || 0;
        });
        f('投注用户', function(d) {
            return d.userBet || 0;
        });

        var c = echarts.init($("#yhsj")[0]);
        var option = {
            title: {
                text: "用户数据",
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['累积用户', '新增用户', '充值用户', '新增充值用户', '登录用户', '投注用户'],
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['6天前', '5天前', '4天前', '3天前', '前天', '昨天', '今天',],
            },
            yAxis: {
                type: 'value',
            },
            series: series,
        };
        c.setOption(option)
    }

    $(".charge a").click(function() {
        Util.addPopUp("charge_stat", "charge_stat", 700);
    })
</script>